//
//  MYXInterestViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/13.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MYXInterestViewController.h"


@interface MYXInterestViewController ()<UITextViewDelegate>


@property (weak, nonatomic) IBOutlet UITextField *uitextFiled;

@end

@implementation MYXInterestViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}


-(void)right
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"添加" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(0, 0, 60, 44);
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [self.navigationItem setRightBarButtonItem:cancelItem animated:NO];
    
}
-(void)add{
    
    //为了防止键盘的闪屏，延迟0.25秒退出
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (self.InterestBlock) {
             [[NSNotificationCenter defaultCenter] postNotificationName:@"info" object:nil];
        }
        self.InterestBlock(self.uitextFiled.text);
        [self.navigationController popViewControllerAnimated:YES];
        
    });
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self right];
    //添加键盘监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfieldChange) name:UITextFieldTextDidChangeNotification object:self.uitextFiled];
}

- (void)textfieldChange{
    if (self.uitextFiled.text.length == 9) {
        self.uitextFiled.text = [self.uitextFiled.text substringToIndex:8] ;
        ShowSVProgressErrorMessage(@"最多只能输入8个字");
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:self.uitextFiled];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
